<?php 
class CartController extends Controller{
	// 首页
	public function index(){
		// p($_SESSION);
		// 显示模版
		$this->display();
	}

	// ajax更新购物车数据
	public function ajax_update(){
		// 获得数据
		$data = Q('post.');
		// 更新购物车
		Cart::update($data);
		$data = Cart::getAllData();
		$this->ajax($data);
	}

	// ajax清除选中商品数据
	public function ajax_choose(){
		$sidArr = explode(',', rtrim(Q('post.sid'),','));
		foreach ($sidArr as $v) {
			$data = array(
				'sid'=>$v,
				'num'=>0
				);
			Cart::update($data);
		}
		$data = Cart::getAllData();
		$this->ajax($data);
	}

	// ajax添加购物车
	public function ajax_cart(){
		// p(Q('post.'));
		// 获得当前商品gid,可以找到当前商品的信息
		$gid = Q('post.gid',0,'intval');
		// 获得当前商品信息
		$good = K('Goods')->where("gid={$gid}")->find();
		$gname = $good['gname'];
		$price = Q('post.price');
		// 组合id,把逗号去掉，分成数组
		$group =explode(',', ltrim(Q('post.group'),','));
		// 获得当前商品对应的商品属性
		$goodAttr = K('Goods_attr')->where("f_goods_gid={$gid}")->all();
		// 通过组合id找到货品列表glid
		$groupStr = ltrim(Q('post.group'),',');
		$glid = K('Goods_list')->where("`group`='{$groupStr}'")->getField('glid');
		// 循环数组找到当前组合id分别对应的文字
		$attr = array();
		foreach ($group as $k => $v) {
			foreach ($goodAttr as $va) {
				if ($k==0) {
					if ($va['gaid']==$v) {
						$attr['color'] = $va['gavalue'];
						$attr['glid'] = $glid;
					}
				}elseif ($k==1) {
					if ($va['gaid']==$v) {
						$attr['size'] = $va['gavalue'];
					}
				}
			}
		}
		$attr['logo'] = $good['gpic'];
		$attr['old_price'] = $good['old_price'];
		// 组成数组
		$data = array(
			'id'		=>	$gid,
			'name'		=>	$gname,
			'num'		=>	1,
			'price'		=>	$price,
			'options'	=>	$attr
			);
		cart::add($data); // 添加到购物车 
		// p($_SESSION);
		$this->ajax($_SESSION);
	}

}	















 ?>